//
//  SQLiteView.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2018/5/4.
//  Copyright © 2018年 Lssyyy. All rights reserved.
//

#import "SQLiteView.h"
@interface SQLiteView()
- (IBAction)btnMethod:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *idTF;
@property (weak, nonatomic) IBOutlet UITextField *scoreTF;
@property (weak, nonatomic) IBOutlet UITextField *ageTF;
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation SQLiteView

- (void)awakeFromNib{
    [super awakeFromNib];
    NSLog(@"%s",__func__);
    [self setUI];
}

- (void)setUI{
    
}

+ (instancetype)SQLiteView{
    NSLog(@"%s",__func__);
    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}

- (void)showResult:(SQLiteStudentModel *)model{
    _textView.text = [_textView.text stringByAppendingString:[NSString stringWithFormat:@"%d==%@==%d==%f",model.id,model.name,model.age,model.score]];
}

- (IBAction)btnMethod:(UIButton *)sender {
    SQLiteStudentModel *model = [[SQLiteStudentModel alloc]init];
    
    if ([sender.titleLabel.text isEqualToString:@"增"]) {
        if (kStringIsEmpty(_nameTF.text)||kStringIsEmpty(_ageTF.text) || kStringIsEmpty(_scoreTF.text)) {
//            NSAssert(NO, @"参数错误啊");
            [self alertWithNotice:@"参数错误"];
        }else{
            model.sqlType = SqlTypeAdd;
            model.name = _nameTF.text;
            model.age = [_ageTF.text intValue];
            model.score = [_scoreTF.text floatValue];
            if ([_delegate respondsToSelector:@selector(updateSqlString:)]) {
                [_delegate updateSqlString:model];
            }
        }
    }else if ([sender.titleLabel.text isEqualToString:@"删"]){
        if (!kStringIsEmpty(_nameTF.text)||kStringIsEmpty(_ageTF.text) || !kStringIsEmpty(_scoreTF.text)|| !kStringIsEmpty(_idTF.text)) {
            model.sqlType = SqlTypeDel;
            model.id = [_idTF.text intValue];
            model.name = _nameTF.text;
            model.age = [_ageTF.text intValue];
            model.score = [_scoreTF.text floatValue];
            if ([_delegate respondsToSelector:@selector(updateSqlString:)]) {
                [_delegate updateSqlString:model];
            }
        }else{
             [self alertWithNotice:@"参数错误"];
            
        }
    }else if ([sender.titleLabel.text isEqualToString:@"改"]){
        if (!kStringIsEmpty(_nameTF.text)||kStringIsEmpty(_ageTF.text) || !kStringIsEmpty(_scoreTF.text)|| !kStringIsEmpty(_idTF.text)) {
            model.sqlType = SqlTypeChange;
            model.name = _nameTF.text;
            model.age = [_ageTF.text intValue];
            model.score = [_scoreTF.text floatValue];
            if ([_delegate respondsToSelector:@selector(updateSqlString:)]) {
                [_delegate updateSqlString:model];
            }
        }else{
            [self alertWithNotice:@"参数错误"];
        }
    }else{
        if (!kStringIsEmpty(_nameTF.text)||kStringIsEmpty(_ageTF.text) || !kStringIsEmpty(_scoreTF.text)|| !kStringIsEmpty(_idTF.text)) {
            model.sqlType = SqlTypeLook;
            model.id = [_idTF.text intValue];
            model.name = _nameTF.text;
            model.age = [_ageTF.text intValue];
            model.score = [_scoreTF.text floatValue];
            if ([_delegate respondsToSelector:@selector(updateSqlString:)]) {
                [_delegate updateSqlString:model];
            }
        }else{
            [self alertWithNotice:@"参数错误"];
            
        }
    }
}

- (void)alertWithNotice:(NSString *)noticeStr{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:noticeStr preferredStyle:UIAlertControllerStyleAlert];
    
    UIViewController *alertController = [[UIViewController alloc]init];
    [self addSubview:alertController.view];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertController.view removeFromSuperview];
    }];
    [alert addAction:action];
    
    [alertController presentViewController:alert animated:YES completion:nil];
}
@end
